﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HRMS_Model;
using DataManager;
using System.Diagnostics;
using System.Web.Mvc;
using HRMS.Models.ModelBinders;

namespace HRMS.Models
{
    public class JobsModels
    {
        public static bool AddJob(Job job)
        {
            using (var jobEntity = new HRMSEntities())
            {
                jobEntity.Jobs.AddObject(job);
                jobEntity.SaveChanges();
                return true;
            }
        }

        public static List<Job> GetJobs(string sortExpression, string sortDirection, int pageIndex, int pageSize, int pageCount)
        {
            using (var jobEntity = new HRMSEntities())
            {
                return jobEntity.Jobs.OrderBy("it." + sortExpression + " " + sortDirection).Skip(pageIndex * pageSize).Take(pageCount * pageSize).ToList();
            }

        }

        public static int GetJobsCount()
        {
            using (var jobEntity = new HRMSEntities())
            {
                return jobEntity.Jobs.Count();
            }
        }

        public static bool UpdateJobs([ModelBinder(typeof(CustomModelBinder))]Job job)
        {
            try
            {
                using (var jobEntity = new HRMSEntities())
                {
                    var jobToEdit = jobEntity.Jobs.FirstOrDefault(r => r.JobId == job.JobId);
                    if (jobToEdit != null)
                    {
                        jobToEdit.ContactId = job.ContactId;
                        jobToEdit.StartDate = job.StartDate;
                        jobToEdit.EndDate = job.EndDate;
                        jobToEdit.Status = job.Status;
                        jobToEdit.ExperienceReq = job.ExperienceReq;
                        jobToEdit.NoOfVacancies = job.NoOfVacancies;
                        jobToEdit.JobDescription = job.JobDescription;
                         
                        //empToEdit.Department.DepartmentName
                    }
                    jobEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public static bool DeleteJobs(int id)
        {
            try
            {
                using (var jobEntity = new HRMSEntities())
                {
                    jobEntity.Jobs.DeleteObject(jobEntity.Jobs.First(e => e.JobId == id));
                    jobEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }



         
    }
}
        
    
